// pages/selectWord/selectWord.js
const app = getApp()
var base_url = app.globalData.url
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bg:true,
    selectword:'',
    selectwordlst:[]

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.getStorage({
      key: 'exerciseInfo',
      success: function (res) {
        that.setData({
          exerciseInfo:res.data
        })
        wx.request({
          url: base_url+'/getCourseWords/',
          method: 'POST',
          data: {
            book: res.data.book,
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function (res) {
            that.setData({
              allDatas: res.data['allData'],
              
            })
          }
        })
      },
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  change:function(){
    
    this.setData({
      bg: false
    })
  },
  checkButtonTap: function (e) {
    // var that = this
    console.log(e)
    let id = e.currentTarget.dataset.id
    var strs = new Array(); //定义一数组
    strs = id.split(",");
    console.log(id)
    for (let i = 0; i < this.data.allDatas[parseInt(strs[0])]['words'].length; i++) {
      if (this.data.allDatas[parseInt(strs[0])]['words'][i].id == parseInt(strs[1])) {
        if (this.data.allDatas[parseInt(strs[0])]['words'][i].checked == true) {
          this.data.allDatas[parseInt(strs[0])]['words'][i].checked = false;
          for (var i = 0; i < this.data.selectwordlst.length;i++){
            if (this.data.selectwordlst[i] == e.currentTarget.dataset.name)
            this.data.selectwordlst.splice(i,1)
          }
          
        } else {
          this.data.allDatas[parseInt(strs[0])]['words'][i].checked = true;
          this.data.selectwordlst.push(e.currentTarget.dataset.name)
          // this.setData({
          //   selectword: this.data.selectword+e.currentTarget.dataset.name,

          // })
        }
      }
    }
    console.log(this.data.selectwordlst)
    this.setData({
      allDatas: this.data.allDatas,
      msg: "id:" + id
    })
    var strbtn = ''
    for (var i = 0; i < this.data.selectwordlst.length; i++){
      if (i == this.data.selectwordlst.length-1){
        strbtn += this.data.selectwordlst[i]
      }else{
        strbtn += this.data.selectwordlst[i]
        strbtn += '、'
      }
      

    }
    this.setData({
      selectword: strbtn,
    })
  },
  gotoshow:function(){
    var that = this;
    if (that.data.selectword == ''){
      wx.showModal({
        title: '警告',
        content: '请选择生字',
        showCancel: false,
        confirmText: '确定',
        success: function (res) {
        }
      })
      return;
    }
    that.data.exerciseInfo.words = that.data.selectword
    wx.setStorage({
      key: 'allExerciseInfo',
      data: that.data.exerciseInfo,
      success: function (res) {
        wx.navigateTo({
          url: '../showExercise/showExercise',
          success: function (res) {
            console.log(res)
          },
          fail: function () {
          }
        })
      }
    })
  }
})